import axios from 'axios';
import store from './store';

const instance = axios.create({
  baseURL: 'https://mallapi.duyiedu.com/',
});
// 请求拦截
instance.interceptors.request.use((conf) => {
  const config = conf;
  if (config.url.startsWith('/passport')) return config;
  const { params = {} } = config;
  Object.assign(params, { appkey: store.state.userInfo.appkey });
  config.params = params;
  return config;
}, (error) => Promise.reject(error));
// 响应拦截
instance.interceptors.response.use((response) => {
  const { data } = response;
  if (data.status === 'fail') {
    window.$message.error(data.msg);
    return null;
  }
  return data.data;
}, (error) => Promise.reject(error));

export default instance;
